package model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.DBTool;

/**
 * @author 
 * 存放页面信息的类（当前页码，总页数，总记录数， 一页显示的页数等）
 */
/**
 * @author Administrator
 *
 */
public class Page {
	
	//当前页码
	private int currentPage;
	//一页显示的记录数
	private int pageSize;	
	// 记录总数
	private int rowCount; 	
	// 总页数
	private int pageCount;
	// 保存当前页面的数据段的上限
	private int startRow = 0;
	// 保存当前页面的数据段的下限
	private int endRow = 0; 	
	
	
	public Page() {
		
	}
	
	public Page getPage(String tableName ,int currentPage,int pageSize) {

		Page page = new Page();
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);	
		
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		DBTool dbt = DBTool.getDBTool();
		String sql = "select count(*) from " + tableName;

		try {
			pstmt = dbt.getPstmt(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				page.setRowCount(rs.getInt(1));
			}
			if(page.getRowCount()%page.getPageSize()==0){   //设置页总数
				page.setPageCount(page.getRowCount()/page.getPageSize());
			}else{
				page.setPageCount(page.getRowCount()/page.getPageSize() + 1);
			}
			
			page.setStartRow((page.getCurrentPage() - 1 ) * page.getPageSize());
			page.setEndRow(page.getCurrentPage() * page.getPageSize());
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbt.close(rs);
			dbt.close(pstmt);
			dbt.closeConn();
		}
		return page;
		
	}
	
	public Page getPageDistinct(String sql ,int currentPage,int pageSize) {

		Page page = new Page();
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);	
		
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		DBTool dbt = DBTool.getDBTool();
	//	String sql = "select count(*) from " + tableName;

		try {
			pstmt = dbt.getPstmt(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				page.setRowCount(rs.getInt(1));
			}
			if(page.getRowCount()%page.getPageSize()==0){   //设置页总数
				page.setPageCount(page.getRowCount()/page.getPageSize());
			}else{
				page.setPageCount(page.getRowCount()/page.getPageSize() + 1);
			}
			
			page.setStartRow((page.getCurrentPage() - 1 ) * page.getPageSize());
			page.setEndRow(page.getCurrentPage() * page.getPageSize());
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbt.close(rs);
			dbt.close(pstmt);
			dbt.closeConn();
		}
		return page;
		
	}
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getStartRow() {
		return startRow;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	public int getEndRow() {
		return endRow;
	}
	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String toString() {
		return super.toString();
	}
	
}
